using System;
using System.Collections.Generic;
//using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using LynxBlogAPI.Models;

namespace LynxBlogAPI.Controllers
{
    public class PostDataController : ApiController
    {
        private LynxWebLog_Context db = new LynxWebLog_Context();

        // GET api/PostData
        [Queryable]
        public IEnumerable<PostData> GetPostData()
        {
            return db.PostDatas.AsEnumerable();
        }

        // GET api/PostData/5
        [Queryable]
        public PostData GetPostData(Int32 id)
        {
            PostData postdata = db.PostDatas.Find(id);
            if (postdata == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return postdata;
        }

        // PUT api/PostData/5
        public HttpResponseMessage PutPostData(Int32 id, PostData postdata)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != postdata.postid)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(postdata).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // POST api/PostData
        public HttpResponseMessage PostPostData(PostData postdata)
        {
            if (ModelState.IsValid)
            {
                db.PostDatas.Add(postdata);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, postdata);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = postdata.postid }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }

        // DELETE api/PostData/5
        public HttpResponseMessage DeletePostData(Int32 id)
        {
            PostData postdata = db.PostDatas.Find(id);
            if (postdata == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.PostDatas.Remove(postdata);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK, postdata);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}